<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login-form" label-width="auto" :model="form" ref="formRules" :rules="rules">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item class="form-item" prop="username">
            <el-input v-model="form.username" placeholder="请输入账号" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item class="form-item" prop="password">
            <el-input v-model="form.password" show-password type="password" placeholder="请输入密码"
              :prefix-icon="Lock"></el-input>
          </el-form-item>
          <el-form-item class="form-item-botton">
            <el-button :loading="btnS" :disabled="btnS" class="login-btn" type="primary" @click="loginB">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
// import {useUserStore} from '@/stores/modules/user'
import { useUserStore } from '../../stores/modules/user';
import { useRouter } from 'vue-router';
import { ElNotification } from 'element-plus';
import { getTime } from '../../utils/time';
const useStore = useUserStore()
//获取路由器
const router = useRouter()
//表单
const form = ref({
  password: '111111',
  username: 'admin'
})
//表单校验
const rules = ref({
  username: [
    { min: 3, max: 5, message: '账号长度至少五位', trigger: 'blur' },
  ],
  password: [{ min: 3, max: 6, message: '密码长度至少6位', trigger: 'blur' },]
})
//获取el-form的表单元素
const formRules = ref()
//按钮初始加载效果
const btnS = ref(false)
//按钮
const loginB = async () => {
  // console.log(formRules, '6666')
  //保证所有的表单项校验通过再发请求
  await formRules.value.validate()
  //开始加载
  btnS.value = true
  try {
    //保证登录成功
    await useStore.actionLogin(form.value)
    //跳转到首页
    router.push('/')
    //登录成功提示信息
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `Hi,${getTime()}好`
    })
    //登录成功后加载状态消失
    //开始加载
    btnS.value = false
  } catch (error: any) {
    //登录失败，加载效果消失
    btnS.value = false
    // console.log(error, '66')
    ElNotification({
      type: 'error',
      message: error
    })
  }

}
</script>

<style scoped lang="scss">
.login-container {
  position: relative;
  width: 100%;
  //浏览器视口高度
  height: 100vh;
  //no-repeat 控制了背景图像是否应该在水平和垂直方向上重复 no-repeat表示不重复
  background: url('./../../assets/images/background.jpg') no-repeat;
  //当使用 cover 值时，浏览器会自动调整背景图像的大小，以便图像能够完全覆盖其所在的背景区域，同时保持图像的宽高比不变，防止图像变形。
  background-size: cover;

  .login-form {
    position: absolute;
    position: relative;
    width: 80%;
    top: 35vh;
    background: url('./../../assets//images//login_form.png') no-repeat;
    background-size: cover;
    margin-left: 40px;

    h1 {
      color: white;
      font-size: 34px;
      padding: 15px 0px;
      margin-left: 15px;
    }

    h2 {
      color: white;
      font-size: 25px;
      padding: 25px 0px;
      margin-left: 15px;
    }

    .form-item {
      margin-left: 40px;
      width: 400px;
    }

    .login-btn {
      width: 400px;
      margin-left: 40px;
      margin-bottom: 30px;
    }
  }
}
</style>
